<template>
  <div class="Customerrfm_container">
    <!-- 搜索条件 -->
    <div class="nav">
      <Row>
        <Col span="24">
          <h2 class="addcustomergroup_title">用户统计</h2>
        </Col>
      </Row>
      <Row>
        <Col span="24">
          <div class="Firstregis">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="90">
              <Row>
                <Col span="5">
                  <FormItem label="选择颗粒度：">
                    <Row>
                      <Col span="5">
                        <RadioGroup type="button" v-model="formValidate.time_type" class="buttongroup">
                          <Radio label="1">日</Radio>
                          <Radio label="2">周</Radio>
                          <Radio label="3">月</Radio>
                          <Radio label="4">年</Radio>
                        </RadioGroup>
                      </Col>
                    </Row>
                  </FormItem>
                </Col>
                <Col span="6" push="1">
                  <FormItem label="选择时间：">
                    <Row>
                      <Col span="5">
                        <FormItem prop="start_time" :label-width="120">
                          <DatePicker
                            type="datetime"
                            :value="formValidate.start_time"
                            @on-change="formValidate.start_time=$event"
                            format="yyyy-MM-dd"
                            placeholder="请选择日期"
                            style="width:9vw;"
                          ></DatePicker>
                        </FormItem>
                      </Col>
                      <Col span="7" style="text-align: center" push="10">~</Col>
                      <Col span="6" push="10">
                        <FormItem prop="end_time" :label-width="120">
                          <DatePicker
                            type="datetime"
                            :value="formValidate.end_time"
                            @on-change="formValidate.end_time=$event"
                            format="yyyy-MM-dd"
                            placeholder="请选择日期"
                            style="width:9vw;"
                          ></DatePicker>
                        </FormItem>
                      </Col>
                    </Row>
                  </FormItem>
                </Col>
                <Col span="8" push="5" style="margin-left:1vw;">
                  <FormItem label="选择会员渠道：" prop="channel_id" :label-width="100">
                    <Select v-model="formValidate.channel_id" placeholder="请选择.." style="width:11vw;margin-left:-10px;" clearable>
                      <Option value="0">全部</Option>
                      <Option
                        v-for="item in channels"
                        :value="item.id"
                        :key="item.value"
                      >{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                </Col>
              </Row>
              <Row  style="margin-top:2vh;">
                <Col span="8">
                  <Button
                    type="success"
                    style="width:10vw;"
                    @click="getReportMemberStatementList"
                  >查询</Button>
                </Col>
                <Col span="3" push="10">
                  <Button
                    type="success"
                    style="width:10vw;margin-left:1vw;"
                    @click="exportData()"
                  >下载</Button>
                </Col>
              </Row>
            </Form>
          </div>
        </Col>
      </Row>
    </div>
    <div class="chart_container" v-if="showchart == true">
      <div id="Userstatistics" v-if="showchart == true"></div>
    </div>
    <div>
      <Table :columns="columns" height="300" :data="formList" border size="small" ref="table" style="width:100%;margin-top:5vh;"></Table>
    </div>
  </div>
</template>
<script>
import {
  getReportMemberStatement,
  exportMemberStatement,
  fetchChannelList
  // downloadtable
} from "@/api/report";
import echarts from "echarts";
export default {
  name: "Userstatistics",
  data() {
    return {
      showchart:false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      reportList: [],
      timeList: [],
      formList: [],
      nameList: [],
      channels: [],
      endtime:'',
      starttime:'',
      formValidate: {
        // 查询参数
        start_time: "",
        end_time: "",
        channel_id: '0',
        store: "",
        time_type: "1"
      },
      ruleValidate: {
        // 查询验证
        selectdate: [
          {
            required: true,
            type: "date",
            message: "请选择时间",
            trigger: "change"
          }
        ],
        selecttime: [
          {
            required: true,
            type: "date",
            message: "请选择时间",
            trigger: "change"
          }
        ],
        store: [
          { required: true, message: "请选择店铺类型", trigger: "change" }
        ],
        officialaccount: [
          { required: true, message: "请选择公众号", trigger: "change" }
        ],
        membershipchannel: [
          { required: true, message: "请选择会员渠道", trigger: "change" }
        ]
      },
      columns: [
        {
          title: "开始截止时间",
          key: "date_str",
          align: "center",
          minWidth:150,
        },
        {
          title: "用户总人数",
          key: "total_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "顾客数",
          key: "purchase_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "顾客占比%",
          key: "purchase_percent",
          align: "center",
          minWidth:150,
        },
        {
          title: "潜客数",
          key: "no_purchase_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "潜客占比%",
          key: "no_purchase_percent",
          align: "center",
          minWidth:150,
        },
        {
          title: "会员数",
          key: "member_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "会员占比%",
          key: "member_percent",
          align: "center",
          minWidth:150,
        },
        {
          title: "非会员数",
          key: "no_member_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "非会员数占比%",
          key: "no_member_percent",
          align: "center",
          minWidth:150,
        },
        {
          title: "粉丝数",
          key: "fans_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "粉丝数占比%",
          key: "fans_percent",
          align: "center",
          minWidth:150,
        },
        {
          title: "非粉丝数",
          key: "no_fans_count",
          align: "center",
          minWidth:150,
        },
        {
          title: "非粉丝数占比%",
          key: "no_fans_percent",
          align: "center",
          minWidth:150,
        }
      ]
    };
  },
  created() {
    this.channelList();
  },
  methods: {
     getNowFormatDate() {
      var date = moment().format("YYYY-MM-DD"); //当前时间
      console.log(date)
      var CurDate = moment().subtract(2, "days").format("YYYY-MM-DD"); //当前时间的前2天时间
      var startDate = moment().subtract(5, "days").format("YYYY-MM-DD"); //当前时间的前5天时间
      this.formValidate.start_time = startDate;
      this.formValidate.end_time = CurDate;
    }, 

    handleSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.$Message.success("查询成功!");
        } else {
          this.$Message.error("查询失败,请补充查询条件");
        }
      });
    },

    channelList() {
      fetchChannelList().then(res => {
        if (res.data.code == 200) {
          if (res.data.data) {
            this.channels = res.data.data;
          }
        } else {
          this.$Message.error("操作失败");
        }
      });
    },
    // 折线图
    drawUserstatistics() {
      echarts.dispose(document.getElementById("Userstatistics"));
      this.Userstatistics = echarts.init(document.getElementById("Userstatistics"));
      this.Userstatistics.setOption({
        color:['#14CAF4','#00D7B0','#F59A76','#5684FF','#EDF760','#FC7CA1'],
        animationDelay: function(idx) {
          return idx * 150;
        },
        grid: {
          top: 40
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999"
            }
          },
        },
        legend: {
          top: "95%",
          data: this.nameList
        },
        axisPointer: {},
        xAxis: [
          {
            type: "category",
            data: this.timeList,
            axisPointer: {
              type: "shadow"
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            name: "",
            axisLabel: {},
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              formatter: function(value) {
                return value / 10000 + "万";
              }
            },
          },
          {
            type: "value",
            name: "",
            min: 0,
            interval: 50000,
            axisLabel: {
              formatter: "{value} %"
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
          }
        ],
        series: [
          {
            name: "顾客数量",
            type: "bar",
            stack: '堆叠',
            barWidth:'50px',
            data: this.reportList[0].data
          },
          {
            name: "会员数量",
            type: "bar",
            stack: '堆叠',
            barWidth:'50px',
            data: this.reportList[1].data
          },
          {
            name: "粉丝数量",
            type: "bar",
            stack: '堆叠',
            barWidth:'50px',
            data: this.reportList[2].data
          },
          {
            name: "顾客占比",
            type: "line",
            yAxisIndex: 1,
            data: this.reportList[3].data
          },
          {
            name: "会员占比",
            type: "line",
            yAxisIndex: 1,
            data: this.reportList[4].data
          },
          {
            name: "粉丝占比",
            type: "line",
            yAxisIndex: 1,
            data: this.reportList[5].data
          }
        ]
      });
    },
    drawCharts() {
      this.drawUserstatistics();
    },
    exportData() {
      this.$refs.table.exportCsv({
          filename: "用户统计数据数据报表"
        });
    },
    getReportMemberStatementList() {
      this.TableLoading = true;
      this.load = true;
      if (this.formValidate.time_type == ''){
        this.$Message.error("请选择颗粒度");
        return;
      }
      if (this.formValidate.start_time == "") {
        this.$Message.error("请选择时间段");
        return;
      }
      if (this.formValidate.end_time == "") {
        this.$Message.error("请选择时间段");
        return;
      }
      if(this.formValidate.start_time > this.formValidate.end_time){
        this.$Message.error('开始时间必须小于结束时间!');
        return;
      }
      var reqParams = {
        time_type: this.formValidate.time_type,
        start_time: this.formValidate.start_time,
        end_time: this.formValidate.end_time,
        channel_id: this.formValidate.channel_id
      };
      if(this.formValidate.channel_id == 0){
        reqParams.channel_id = '';
      }
      getReportMemberStatement(reqParams)
        .then(res => {
          this.TableLoading = false;
          if (res.data.code == 200) {
            this.reportList = res.data.result.reportLists;
            this.timeList = res.data.result.timeList;
            this.formList = res.data.result.formList;
            this.nameList = res.data.result.nameList;
            this.showchart = true;
            setTimeout(()=>{
              this.drawCharts();
            },500)
          }else if(res.data.code == 401){
            this.$Message.error(res.data.msg);
          }else {
          this.$Message.error("操作失败");
        }
        })
        .catch(error => {
          this.$Message.error(error);
        });
    }
  },
  mounted() {
    this.getNowFormatDate();
    var time = setInterval(() => {
      this.getNowFormatDate();
    }, 86400000);
    this.getReportMemberStatementList();
  },
};
</script>
<style lang="scss" scoped>
@import "../../styles/common.scss";
.Customerrfm_container {
  @include container_global;
  background-color: #fff;
  .active {
    color: #68b6ff;
    border: 1px solid #8fc3ff;
    background-color: #e6f2ff;
  }
  .nav {
    height: 7vh;
    width: 100%;
    line-height: 35px;
    background-color: #fff;
    position: relative;
    .addcustomergroup_title {
      height: 7vh;
      font-weight: bolder;
    }
    .buttongroup {
      display: flex;
      justify-content: flex-start;
    }
  }
  .chart_container {
    // 图表容器
    width: 100%;
    height: 60vh;
    position: relative;
    margin-top: 20vh;
    border: 1px solid #e6f2ff;
    #Userstatistics {
      width: 85vw;
      height: 60vh;
    }
  }
  .table {
    min-height: 20vh;
    margin-top: 2vh;
    width: 84vw;
    padding-bottom: 2vh;
  }
  .page {
    @include page_global;
  }
}
</style>
 
